package com.cyan.note.controller.manager;

import com.cyan.note.common.ApiResult;
import com.cyan.note.dto.manager.config.OrderTypeConfigDTO;
import com.cyan.note.dto.manager.config.OrderSourceConfigDTO;
import com.cyan.note.entity.ShopSetting;
import com.cyan.note.service.manager.ManagerSettingService;
import com.cyan.note.util.SecurityUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 店长端-设置配置控制器
 * 
 * @author ZhangzhenYu
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/api/manager/settings")
public class ManagerSettingController {

    @Autowired
    private ManagerSettingService managerSettingService;

    /**
     * 获取店铺所有设置
     */
    @GetMapping("/all")
    public ApiResult<Map<String, String>> getAllSettings() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getAllSettings(shopId);
    }

    /**
     * 按分类获取设置
     */
    @GetMapping("/category/{category}")
    public ApiResult<List<ShopSetting>> getSettingsByCategory(@PathVariable String category) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getSettingsByCategory(shopId, category);
    }

    /**
     * 获取单个设置值
     */
    @GetMapping("/key/{settingKey}")
    public ApiResult<String> getSettingValue(@PathVariable String settingKey) {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getSettingValue(shopId, settingKey);
    }

    /**
     * 更新单个设置
     */
    @PutMapping("/key/{settingKey}")
    public ApiResult<Void> updateSetting(
            @PathVariable String settingKey,
            @RequestParam String settingValue
    ) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.updateSetting(shopId, settingKey, settingValue, operatorId);
    }

    /**
     * 批量更新设置
     */
    @PutMapping("/batch")
    public ApiResult<Void> batchUpdateSettings(@Valid @RequestBody List<ShopSetting> settings) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.batchUpdateSettings(shopId, settings, operatorId);
    }

    /**
     * 重置分类设置为默认值
     */
    @PostMapping("/reset/{category}")
    public ApiResult<Void> resetCategorySettings(@PathVariable String category) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.resetCategorySettings(shopId, category, operatorId);
    }

    /**
     * 获取订单类型配置
     */
    @GetMapping("/order-types")
    public ApiResult<List<OrderTypeConfigDTO>> getOrderTypeConfig() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getOrderTypeConfig(shopId);
    }

    /**
     * 更新订单类型配置
     */
    @PutMapping("/order-types")
    public ApiResult<Void> updateOrderTypeConfig(@Valid @RequestBody List<OrderTypeConfigDTO> configs) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.updateOrderTypeConfig(shopId, configs, operatorId);
    }

    /**
     * 获取订单来源配置
     */
    @GetMapping("/order-sources")
    public ApiResult<List<OrderSourceConfigDTO>> getOrderSourceConfig() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getOrderSourceConfig(shopId);
    }

    /**
     * 更新订单来源配置
     */
    @PutMapping("/order-sources")
    public ApiResult<Void> updateOrderSourceConfig(@Valid @RequestBody List<OrderSourceConfigDTO> configs) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.updateOrderSourceConfig(shopId, configs, operatorId);
    }

    /**
     * 获取系统参数设置
     */
    @GetMapping("/system")
    public ApiResult<Map<String, Object>> getSystemSettings() {
        Long shopId = SecurityUtils.getCurrentShopId();
        return managerSettingService.getSystemSettings(shopId);
    }

    /**
     * 更新系统参数设置
     */
    @PutMapping("/system")
    public ApiResult<Void> updateSystemSettings(@RequestBody Map<String, Object> settings) {
        Long shopId = SecurityUtils.getCurrentShopId();
        Long operatorId = SecurityUtils.getCurrentUserId();
        return managerSettingService.updateSystemSettings(shopId, settings, operatorId);
    }
}